package casa.depesos.app.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import casa.depesos.app.com.BuildConfig;
import casa.depesos.app.com.R;
import casa.depesos.app.com.databinding.ActivityCheckBinding;
import casa.depesos.app.com.frag.NetFailFragment;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.bean.ProgressBean;
import casa.depesos.app.com.http.bean.ScoreBean;
import casa.depesos.app.com.http.req.IsScoreReq;
import casa.depesos.app.com.http.req.ProgressReq;
import casa.depesos.app.com.http.req.VerifyReq;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;


public class CheckActivity extends BaseActivity {

    ActivityCheckBinding binding;

    @Override
    protected boolean needPadding() {
        return false;
    }

    final String title = "Resultado de la solicitud";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCheckBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.top.title.setText(title);
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        binding.top.back.setOnClickListener(v -> finish());
        binding.refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                checkStatus(false);
            }
        });

        Report.show("ACTION_PP_003");
        getData(false);
    }

    public void getData(boolean fromFail) {
        showLoading();
        clearFragment(getSupportFragmentManager());
        if (fromFail) {
            Report.click("ACTION_PP_002");
        }
        if (Constant.white) {
            h.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Utils.setStatus(62);
                    Intent i = new Intent(CheckActivity.this, NoActivity.class);
                    startActivity(i);
                    finish();
                }
            }, BuildConfig.DEBUG ? 5000 : 60 * 1000);
            return;
        }
        checkVerify();
        score();
    }

    protected void checkVerify() {
        new VerifyReq().req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                if (success) {
                    checkStatus(true);
                } else {
                    hideLoading();
                    Report.show("ACTION_PP_001");
                    replaceFragment(R.id.frame, new NetFailFragment().setTitle(title));
                }
            }
        });
    }

    protected void checkStatus(boolean showLoading) {
        if (showLoading) {
            showLoading();
        }
        new ProgressReq().req(new ReqCallback<ProgressBean>() {
            @Override
            public void callback(boolean success, ProgressBean bean) {
                binding.refresh.setRefreshing(false);
                if (success && bean.isValid()) {
                    check(bean.data.status);
                } else {
                    hideLoading();
                    replaceFragment(R.id.frame, new NetFailFragment().setTitle(title));
                }
            }
        });
    }

    private void check(int status) {
        if (status == 60) {
            hideLoading();
            Intent i = new Intent(CheckActivity.this, OkActivity.class);
            startActivity(i);
            finish();
        } else if (status == 62) {
            hideLoading();
            Intent i = new Intent(CheckActivity.this, NoActivity.class);
            startActivity(i);
            finish();
        } else {
            h.postDelayed(() -> checkStatus(false), 10 * 1000);
        }
    }

    private void score() {
        new IsScoreReq().req(new ReqCallback<ScoreBean>() {
            @Override
            public void callback(boolean success, ScoreBean bean) {
                if (success && bean.isValid()) {
                    if ("1".equalsIgnoreCase(bean.info.open)) {
                        binding.score.setVisibility(View.VISIBLE);
                        binding.score.setOnClickListener(v -> {
                            Report.click("ACTION_PP_004");
                            WebActivity.startGooglePlay(bean.info.link);
                        });
                    } else {
                        binding.score.setVisibility(View.INVISIBLE);
                    }
                }
            }
        });
    }
}
